﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Public;
using QLTV.DAL;
using QLTV.BLL;
using QLTV.GUI.Forms;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;

namespace QLTV.GUI.UserControl
{

    public partial class ucQuanLyMuonTra : DevExpress.XtraEditors.XtraUserControl
    {
        PhieuSachDTO psDTO;
        PhieuSachBLL psBLL;
        SachDTO sDTO = new SachDTO();
        public ucQuanLyMuonTra()
        {
            InitializeComponent();
        }

        private void ucQuanLyMuonTra_Load(object sender, EventArgs e)
        {
            psDTO = new PhieuSachDTO();
            psBLL = new PhieuSachBLL();
        }

        private void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            GridView view = sender as GridView;
            //if (e.Column.VisibleIndex == 0 && view.IsMasterRowEmpty(e.RowHandle))
            //{

            //}
        }

        private void btnTraTien_Click(object sender, EventArgs e)
        {
            var row = grdSachQuaHan.FocusedRowHandle;
            ////sDTO.MaSach = grdSachQuaHan.GetRowCellValue(row, "MaSach").ToString();

            ////psDTO.MaDG = txtMaDG.Text.ToString();
            psDTO.MaDG = grdSachQuaHan.GetRowCellValue(row, "MaDG").ToString();
            //DataTable dtTable = psBLL.TimPhieuSach(psDTO);
            //DataTable da = psBLL.HienSachTra(psDTO);
            ////psDTO.MaPhieu = dtTable.Rows[0]["MaPhieu"].ToString();

            //frmPhieuThuTienPhat fPTTP = new frmPhieuThuTienPhat(psDTO.MaDG.ToString(), 
            //                                                    psDTO.MaPhieu.ToString(), 
            //                                                    dtTable.Rows[0]["HoVaTen"].ToString(), 
            //                                                    dtTable.Rows[0]["TongNo"].ToString(), 
            //                                                    DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy")), 
            //                                                    da.Rows[0]["SoNgayMuon"].ToString(), 
            //                                                    decimal.Parse(dtTable.Rows[0]["TienPhatKyNay"].ToString()));
            //fPTTP.ShowDialog();

            //ucPhieuTraSach uc = new ucPhieuTraSach();
            //uc.Show();

            ucPhieuTraSach ucPhieuTra = new ucPhieuTraSach();
            //ucPhieuTra.Dock = DockStyle.Fill;
            //ucPhieuTra.Show();
            this.Controls.Add(ucPhieuTra);



            //Addtabpage(ucPhieuTra, "Thông Tin Phiếu Trả Sách");
            //if (!this.xtraTab.IsLoading || this.xtraTab.Visible == false)
            //{
            //    this.xtraTab.Visible = true;
            //}
        }

        private void btnPhieuDaTra_Click(object sender, EventArgs e)
        {
            groupControl1.Text = "Danh sách phiếu mượn đã trả sách";
            gridCPhieuMuonDaTra.Visible = true;
            gridCPhieuMuonChuaTra.Visible = false;
            gridCSachQuaHan.Visible = false;

            DataSet setDaTra = new DataSet();
            DataTable dtPSDaTra = psBLL.QuanLyMuonTra_LoadPhieuSachDaTra();
            DataTable dtSDaTra = psBLL.QuanLyMuonTra_LoadSachDaTra();
            if (dtPSDaTra.Rows.Count != 0 && dtSDaTra.Rows.Count != 0)
            {
                setDaTra.Tables.Add(dtPSDaTra);
                setDaTra.Tables.Add(dtSDaTra);
                setDaTra.Tables[0].TableName = "LoadPhieuSachDaTra";
                setDaTra.Tables[1].TableName = "LoadSachDaTra";

                DataColumn key = setDaTra.Tables["LoadPhieuSachDaTra"].Columns["MaPhieu"];
                DataColumn fkey = setDaTra.Tables["LoadSachDaTra"].Columns["MaPhieu"];

                DataRelation DRDaTra = new DataRelation("Chi Tiết Phiếu", key, fkey, true);
                setDaTra.Relations.Add(DRDaTra);
                gridCPhieuMuonDaTra.DataSource = setDaTra.Tables[0];
                gridCPhieuMuonDaTra.ForceInitialize();
                CardView cardView2 = new CardView(gridCPhieuMuonDaTra);
                gridCPhieuMuonDaTra.LevelTree.Nodes.Add("Chi Tiết Phiếu", cardView2);
                cardView1.ViewCaption = "Chi Tiết Phiếu Sách";
                grdPhieuMuonDaTra.Columns["MaPhieu"].VisibleIndex = 0;
                cardView2.PopulateColumns(setDaTra.Tables[1]);
                grdPhieuMuonDaTra.OptionsBehavior.Editable = false;
                cardView2.Columns["MaPhieu"].VisibleIndex = 1;
            }
        }

        private void btnPhieuChuaTra_Click(object sender, EventArgs e)
        {
            groupControl1.Text = "Danh sách phiếu mượn chưa trả sách";
            gridCPhieuMuonDaTra.Visible = false;
            gridCPhieuMuonChuaTra.Visible = true;
            gridCSachQuaHan.Visible = false;

            DataSet set = new DataSet();
            DataTable dtPS = psBLL.QuanLyMuonTra_LoadPhieuSachChuaTra();
            DataTable dtS = psBLL.QuanLyMuonTra_LoadSachChuaTra();
            if (dtPS.Rows.Count != 0 && dtS.Rows.Count != 0)
            {
                set.Tables.Add(dtPS);
                set.Tables.Add(dtS);
                set.Tables[0].TableName = "LoadPhieuSach";
                set.Tables[1].TableName = "LoadSach";

                DataColumn k = set.Tables["LoadPhieuSach"].Columns["MaPhieu"];
                DataColumn kt = set.Tables["LoadSach"].Columns["MaPhieu"];
                DataRelation RE = new DataRelation("Chi Tiết Phiếu", k, kt, true);
                set.Relations.Add(RE);
                gridCPhieuMuonChuaTra.DataSource = set.Tables["LoadPhieuSach"];
                gridCPhieuMuonChuaTra.ForceInitialize();

                CardView cardView1 = new CardView(gridCPhieuMuonChuaTra);
                gridCPhieuMuonChuaTra.LevelTree.Nodes.Add("Chi Tiết Phiếu", cardView1);
                cardView1.ViewCaption = "Chi tiết Phiếu sách";

                grdPhieuMuonChuaTra.Columns["MaPhieu"].VisibleIndex = 0;
                cardView1.PopulateColumns(dtS);
                grdPhieuMuonChuaTra.OptionsBehavior.Editable = false;
                cardView1.Columns["MaPhieu"].VisibleIndex = 1;
            }
        }

        private void btnPhieuQuaHan_Click(object sender, EventArgs e)
        {
            groupControl1.Text = "Danh sách phiếu mượn quá hạn trả";
            gridCPhieuMuonDaTra.Visible = false;
            gridCPhieuMuonChuaTra.Visible = false;
            gridCSachQuaHan.Visible = true;

            DataTable psQuaHan = psBLL.LoadPhieuSachQuaHan();
            gridCSachQuaHan.DataSource = psQuaHan;
        }
    }
}
